<?php
        require 'UserRepository.php';
        require 'User.php';
        require 'Message.php';
        require 'MessageRepository.php';
        require 'database.inc.php';
        require 'checkPrivileges.php';
        require_once('smarty3/Smarty.class.php');
        require('googleMapsApi/GoogleMap.php');
        require('googleMapsApi/JSMin.php');
        require 'reverseGeocoding.php';


$smarty = new Smarty();
$messageRepository = new MessageRepository();
$messages = $messageRepository->getAllMessages($db);
$locations = array();
foreach ($messages as $message) {
    $locations[$message['ID']] = reverseGeocode($message['Latitude'], $message['Longitude']);
}
$smarty->assign('messages',$messages);
$smarty->assign('locations',$locations);
$smarty->assign('username',  htmlspecialchars($_SESSION['username']));

$comments = array();
foreach($messages as $message){
    $comments[$message['ID']] = $messageRepository->getCommentsForMessage($db, $message['ID']);    
}
$smarty->assign('post', $_POST);
$smarty->assign('comments',$comments);
//google mapa

$map = new GoogleMapAPI('map');
$map->_minify_js = isset($_REQUEST["min"])?FALSE:TRUE;
//$map->setAPIKey('AIzaSyALjW_amC0TXcJRza0uEmwm2G74UP4SeEY');
foreach($messages as $message){
    if ($message['Longitude'] != 0 && $message['Latitude'] != 0) {
        $map->addMarkerByCoords($message['Longitude'], $message['Latitude'], $message['Username'].'<br/><img src="image.php?image='.$message['PictureID'].'&size=thumb" alt="" height="50" width="50"/>'.$message['Comment']);
    }
    
}
//$map->setCenterCoords(15.978501,45.815005);
//height="295" width="275"
$map->setWidth(400);
$map->setHeight(400);
$smarty->assign('HeaderJS',$map->getHeaderJS());
$smarty->assign('MapJS',$map->getMapJS());
$smarty->assign('Map',$map->getMap());
$smarty->assign('OnLoad',$map->getOnLoad());
$smarty->assign('Sidebar',$map->getSidebar());
$smarty->display('index.tpl');
?>